define(['app','scripts/service/ReadilyPhotographServices','scripts/service/NavigationServices','configModule','swiper'
				, 'scripts/service/ContentServices'], function(app) {

	app.register.controller('discloseFormController',function($routeParams,$scope,$rootScope,$http,$timeout,$sce,
					ReadilyPhotographServices,NavigationServices,layer,$location,SystemConfig,ContentServices){
		
		//是否显示报料类型列表
		$scope.showTypeList = false;
		//录音图标切换
		$scope.showRecordIcon = true;
		//地理位置选择
		$scope.showLocation = false;
		//限制图片上传数9张
		var limitpicnum = 9;
		//查看、删除图片展示
		$scope.showDel = false;
		
		$scope.init = function() {
    		$scope.pid = $routeParams.pid;
    		var params = {
    				'pid'		:	$scope.pid,
    				'pagesize'	:	50,
    				'pagenum'	:	1
    		}
			NavigationServices.navigation.list4client(params,function(data) {//获取展示图
				$scope.Pics = data.items;
				
				//设置分享
				var shareParams = {
					'title' : $rootScope.pc_title,	//分享的标题
					'desc'	: $scope.Pics[0].remark,	//分享的描述
					'link'	: layer.newLink([]),		//分享后点击的链接
					'imgurl': SystemConfig.getConfig().COM_LOGO		//分享的图片
				}
				var _successCallbackTimeline = function(){//分享到朋友圈
					/*BbsServices.bbs.share2Timeline(function(data){
		    				layer.show("分享成功!");
					});*/
					layer.show("分享成功!");
				};
				_successCallbackAppMessage = function(){//分享给朋友
					/*BbsServices.bbs.share2AppMessage(function(data){
		    				layer.show("分享成功!");
					});*/
					layer.show("分享成功!");
				};
				var _errorCallbackTimeline = function(){
					layer.confirm("分享出错啦！请发送调试信息给我们！非常感谢！", function(){
						layer.show("发送调试信息成功!");
					},function(){
						layer.show("发送调试信息失败!");
					});
				};
				_errorCallbackAppMessage = _errorCallbackTimeline;
				layer.share(shareParams, _successCallbackTimeline, _errorCallbackTimeline, _successCallbackAppMessage, _errorCallbackAppMessage);
				
			});
		}
		layer.initUser($scope,function() {
			$scope.init();
			$http({
				url    : '/eimp/readily/phone/initPageParams',
				method : 'get'
			}).success(function(data) {
				$scope.initPageParams = data;
				console.info("initPageParams", data);
			})
			if(!isFocusOn()) {
    			layer.show("您还没有关注公众号，部分功能将无法使用！")
    		}
		});
		
		var j = 0;
		$scope.selectImg = function () {
			//alert("已选择图片数："+j);
			wx.chooseImage({	//拍照、本地选图
			    success: function (res) {
			    	var localIds = res.localIds;
			    	var k=0;
			    	function upload() {
			    		if(j < limitpicnum) {
			    			wx.uploadImage({	//上传选图
			    				localId: localIds[k].toString(), // 需要上传的图片的本地ID，由chooseImage接口获得
			    				//isShowProgressTips: 1, // 默认为1，显示进度提示
			    				success: function (res) {
			    					var serverId = res.serverId; // 返回图片的服务器端ID
			    					ReadilyPhotographServices.readilyPhone.upoload({'media_id':serverId},function(res) {
			    						if(angular.isUndefined($scope.pictures)||$scope.pictures == '') {
			    							$scope.pictures = res;
			    						}else {
			    							$scope.pictures = $scope.pictures +','+ res;
			    						}
			    						$scope.pictures = $scope.pictures.replace(/\"/g, "");
			    						$timeout(function() {
			    							$scope.imgarr = $scope.pictures.split(",");
			    						});
			    					});
			    					j++;
			    					k++;
			    					upload();
			    				},
			    				fail: function (res) {
			    					layer.show(JSON.stringify(res));
			    				}
			    			});
			    		}else {
			    			layer.show("最多上传"+limitpicnum+"张图片！");
			    		}
			    	}
			    	upload();
			    }
		    });
		}
		$scope.viewImg = function(e) {//预览图片
			$scope.showDel = true;
			var num = $(e.currentTarget).attr("data-num");
			$('#form').css('display','none');
			//swiper
			var swiper;
			if(num != 0) {
				$scope.$on("ngRepeatLoad", function() {
					swiper = new Swiper('.swiper-container', {
						initialSlide: num,
						centeredSlides: true,
						autoHeight: true
					});
				});
			}else {
				$scope.$on("ngRepeatLoad", function() {
					swiper = new Swiper('.swiper-container', {
						centeredSlides: true,
						autoHeight: true
					});
				});
			}
			
		}
		$scope.stopevt = function(event) {
			event.stopPropagation()
		}
		$scope.hideViewImg = function() {
    		$scope.showDel = false;
    		$('#form').css('display','block');
    	}
		$scope.delImg = function() {//删除图片
			layer.confirm('要删除这张照片吗？',function() {
				var currentImgurl = $(".swiper-slide-active .banner-imgdiv-warp img").attr("src");
				var urlarr = currentImgurl.split("/");
				var imgname = urlarr[urlarr.length-1];
				for(var i=0;i<$scope.imgarr.length;i++) {
					if($scope.imgarr[i] == imgname) {
						$scope.imgarr.splice(i,1);
					}
				}
				$scope.pictures = "";
				for(var i=0;i<$scope.imgarr.length;i++) {
					if(i == 0) {
						$scope.pictures = $scope.imgarr[0];
					}else {
						$scope.pictures = $scope.pictures + "," + $scope.imgarr[i];
					}
				}
				$scope.showDel = false;
				$('#form').css('display','block');
				$scope.$apply();
			});
		}
		$scope.cancel = function() {
			$scope.showDel = false;
			$('#form').css('display','block');
		}
		$scope.record = function () {//开始录音
			layer.confirm('开始录音！',function(){
				$scope.showRecordIcon = false;
				$scope.$apply();
				wx.startRecord({	//开始录音微信接口
					cancel: function () {
				        layer.show('用户拒绝授权录音');
				    }
				});
			});
		}
		$scope.stopRecord = function () {//停止录音
			layer.confirm('停止录音！',function(){
				$scope.showRecordIcon = true;
				wx.stopRecord({	//停止录音微信接口
					success: function (res) {
						var localId = res.localId;
						wx.translateVoice({
							localId: res.localId,	//需要识别的录音ID，由stopRecord接口获得
							complete: function (res) {
								if (res.hasOwnProperty('translateResult')) {
									if(angular.isUndefined($scope.recordResult)||$scope.recordResult == '') {
										$scope.recordResult = res.translateResult;
									}else {
										$scope.recordResult =$scope.recordResult + res.translateResult;	//返回原输入字符+识别的录音字符
									}
									$scope.$apply();	//手动触发angular更改model
									
								} else {
									layer.show('无法识别');
								}
								
							}
						});
					},
					fail: function (res) {
						layer.show(JSON.stringify(res));
					}
				});
			});
		}
		
		$scope.showTip = function(t) {
			switch(t){
				case 1:
					$scope.initPageParams.tipShow = $scope.initPageParams.operat_guide;
					break;
				case 2:
					$scope.initPageParams.tipShow = $scope.initPageParams.incent_mechan;
					break;
				case 3:
					$scope.initPageParams.tipShow = $scope.initPageParams.filing_Rules;
					break;
			}
			ContentServices.fodder.getContent({'id':$scope.initPageParams.tipShow}, function(data){
				$("#tip_popup_content").html(data.content);
				layer.popup("tip_popup_div");
			});
		}
		
		$scope.hotDisclose = function() {//热门报料
			$location.url("/publicParticipation/home?pid="+ $scope.pid +"&flag=true");
		}
		
		$scope.myRecord = function() {//我的记录
			if(isFocusOn()) {
				$location.url("/publicParticipation/home?pid="+ $scope.pid +"&flag=false");
			}else {
				layer.confirm('是否现在关注公众号？',function() {
					$("#qrcode-layer").css("display","block");
				});
			}
		}
		$scope.submit = function() {//报料提交
			if(checked()) {
				if(angular.isUndefined($scope.location)||$scope.location == '') {
					layer.show('请定位地理位置!');
					return false;
//					$scope.location = "无";
				}
				if(angular.isUndefined($scope.typename)||$scope.typename == '') {
					$scope.type = 0;
				}
				var params = {
						'uid'			:	$rootScope.user.id,
						'situation'		:	$scope.recordResult,
						'tipoffType'	:	$scope.type,
						'reportMen'		:	$scope.name,
						'phone'			:	$scope.phone,
						'picurl'		:	$scope.pictures,
						'address'		:	$scope.location
				}
				ReadilyPhotographServices.readilyPhone.add(params,function() {//添加上报信息
					$location.url("/publicParticipation/home?pid="+ $scope.pid +"&flag=false");
					$http({//上报成功后在公众号上打印content的内容
		    			url:'/eimp/readily/phone/sendMessage',
		    			method:'get',
		    			params	:	{
		    				'openId'	:	$rootScope.user.idofchannel,
		    				'channel'	:	$rootScope.user.channel,
		    				'content'	:	'您好，您举报的案件上传成功，我们将尽快审核，感谢您的参与。' 
		    			}
		    			//[20170116]'您好，您举报的事件上传成功，感谢您的参与。' 
		    			//[20170111]'随手拍报料上传成功,感谢您的反馈！'
		    		}).success(function(data) {
			    			//alert("随手拍上传报料成功")
		    		});
				})
			}
		}
    	var checked = function() {//表单验证
    		var mobile = /^\d{11}$/;
			
			if(angular.isUndefined($scope.recordResult)) {
				layer.show('请输入您的上报情况说明!');
				return false;
			}
			if(angular.isUndefined($scope.pictures)) {
				layer.show('请上传您要上报的图片!');
				return false;
			}
			if(angular.isUndefined($scope.phone)) {
				layer.show('请输入您的手机号码');
				return false;
			}
			if(!mobile.test($scope.phone)) {
				layer.show('手机号码格式输入错误,请输入11位电话号码');
				return false;
			}
			return true;
    	}
    	void function() {//自执行匿名函数，获取我的爆料类型数据
    		$http({
				url		:	'/eimp/readily/phone/getTypes',
				method	:	'get'
			}).success(function(data) {
				$scope.typeList = data;
			})
    	}();
		$scope.selectType = function() {//选择报料类型
    		$scope.hideTypeList();
    		if(angular.isUndefined($scope.typename)){//未选择类型时默认选中左侧列表第一个
    			$(".left_list").find("li").removeClass("left_li_active");
    			$(".left_list li:first-child").addClass("left_li_active");
    			$scope.listItem = $scope.typeList[0];
    		}
			$scope.showTypeList = true;
			$timeout(function(){//左右等高
				$(".right_list").height($(".left_list").height());
			})
		}
    	
    	$scope.leftTypeSelect = function(index,event) {//左侧列表选择，右侧数据切换
    		$(".left_list").find("li").removeClass("left_li_active");
    		$(event.currentTarget).addClass("left_li_active");
    		$scope.listItem = $scope.typeList[index];
    		/*if(index == $scope.typeList.length-1) {
    			$scope.typename = $(event.currentTarget).text();
    			$scope.listItem = "";
    			$scope.type = 0;
    			$scope.showTypeList = false;
    		}else {
    			$scope.listItem = $scope.typeList[index];
    		}*/
    	}
    	
    	$scope.rightTypeSelect = function(index,event) {//右侧列表选择
    		$(".right_list").find("li").removeClass("right_li_active");
    		$(event.currentTarget).addClass("right_li_active");
    		$scope.typename = $(event.currentTarget).text();
    		var type1 = $scope.listItem.list[index].id;
    		$scope.type = parseInt(type1);
    		$scope.showTypeList = false;
    	}
    	$scope.hideTypeList = function() {
    		$(".disclose_type").click(function(event){
    			event.stopPropagation()//阻止冒泡
    		});
    		$scope.showTypeList = false;
    	}
    	
    	$scope.selectLocation = function() {
    		$scope.showLocation = true;
    		layer.getLocation(function(option){
    			var iframe = '<iframe id="location_iframe" frameborder="0" scrolling="no" src="/wap/views/publicParticipation/map.html?lat='+option.latitude+'&lng='+option.longitude+'"></iframe>';
	    		$('.location_select').append(iframe);
	    		$('#location_iframe').height($(window).height()-50);
    		});
    	}
    	$scope.getLocation = function() {//选择地理位置
    		$scope.location = document.getElementById("location_iframe").contentWindow.document.getElementById('company_address').value;
    		$(".location_select").click(function(event){
    			event.stopPropagation()
    		});
    		$scope.showLocation = false;
    		
    	}
    	$scope.closeLocation = function() {
    		$scope.showLocation = false;
    	}
    	
    	function isFocusOn() {//用户是否关注
    		if(angular.isUndefined($rootScope.user) || $rootScope.user == null) {//用户未关注
    			return false;
    		}else {
    			if(($rootScope.user.status & 4) == 4) {//用户已关注
    				return true;
    			}else {//用户取消关注
    				return false;
    			}
    		}
    	}
    	
    	$scope.hideQrcode = function() {
    		$("#qrcode").click(function(e) {
    			e.stopPropagation();
    		});
    		$("#qrcode-layer").css("display","none");
    	}
    	
    	//<------------------解决ios客户端导航栏title不显示情况-------------------------->
		var $body = $('body')
		document.title = $rootScope.pc_title;
		// hack在微信等webview中无法修改document.title的情况
		var $iframe = $('<iframe style="display:none" src="/favicon.ico"></iframe>').on('load', function() {
			setTimeout(function() {
				$iframe.off('load').remove()
			}, 0)
		}).appendTo($body)
		//<------------------end解决ios客户端导航栏title不显示情况-------------------------->
	});

});